package com.bonc.apicreator.ctrl;


import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.bonc.apicreator.constant.BaseConstant;
import com.bonc.apicreator.dao.ModuleDao;
import com.bonc.apicreator.dao.ProjectDao;
import com.bonc.apicreator.entity.Module;
import com.bonc.apicreator.entity.Project;


/**
 * 〈一句话功能简述〉Module的controller层
 *
 * @author liuchang
 * @version 2016年10月8日
 * @see ModuleController
 * @since
 */
@Controller
@RequestMapping("/project/{proId}/module")
public class ModuleController {

    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(ModuleController.class);

    /**
     * moduleDao
     */
    @Autowired
    private ModuleDao moduleDao;

    /**
     * projectDao
     */
    @Autowired
    private ProjectDao projectDao;

    /**
     * Description: 加载页面
     * 
     * @param model
     * @param proId
     * @return String
     * @see
     */
    @RequestMapping(value = "")
    public String list(Model model, @PathVariable Long proId) {
        model.addAttribute("proId", proId);
        Project project = projectDao.findOne(proId);
        model.addAttribute("project", project);

        return "module/module.jsp";

    }

    /**
     * Description: ajax请求 Module列表
     * 
     * @param pageable
     * @param condition
     * @param proId
     * @return Object
     * @see
     */

    @RequestMapping(value = {"/list"})
    @ResponseBody
    public Object module(Pageable pageable, String condition, @PathVariable Long proId) {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<Module> moduleList;
        if (StringUtils.isEmpty(condition)) {
            moduleList = moduleDao.findByProIdOrderBySort(proId, pageable);
        }
        else {
            moduleList = moduleDao.findByProIdAndNameContaining(proId, condition, pageable);
        }
        map.put("list", moduleList.getContent());
        map.put("count", moduleList.getTotalElements());
        map.put("code", BaseConstant.STATE_SUCCESS);
        LOGGER.debug("list response:" + JSON.toJSONString(moduleList));
        return map;
    }

    /**
     * Description: 新增module
     * 
     * @param module
     * @param proId
     * @return String
     * @see
     */
    @RequestMapping(value = {"/save"}, method = RequestMethod.POST)
    public String save(Module module, @PathVariable Long proId) {
        module.setProId(proId);
        module.setCreateDate(new Date());
        moduleDao.save(module);
        return "redirect:/project/{proId}/module";
    }

    /**
     * Description:更新前先查询
     * 
     * @param id
     * @return Object
     * @see
     */
    @RequestMapping(value = {"/findOne"})
    @ResponseBody
    public Object findOne(Long id) {
        Map<String, Object> map = new HashMap<String, Object>();
        Module module = moduleDao.findOne(id);
        map.put("code", BaseConstant.STATE_SUCCESS);
        map.put("message", "成功");
        map.put("module", module);
        return map;

    }

    /**
     * Description: 更新module
     * 
     * @param module
     * @return String
     * @see
     */

    @RequestMapping(value = {"/update"}, method = RequestMethod.POST)
    public String update(Module module) {
        Module updateModule = moduleDao.findOne(module.getId());
        module.setCreateDate(updateModule.getCreateDate());
        moduleDao.save(module);
        return "redirect:/project/{proId}/module";
    }

    /**
     * Description: 删除module
     * 
     * @param id
     * @return Object
     * @see
     */

    @RequestMapping(value = {"/delete"}, method = RequestMethod.POST)
    @ResponseBody
    public Object delete(Long id) {

        moduleDao.delete(id);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", BaseConstant.STATE_SUCCESS);

        return map;

    }

    /**
     * Description: 重复校验
     * 
     * @param name
     * @param proId
     * @param id
     * @return Object
     * @see
     */
    @RequestMapping(value = {"/checkName"})
    @ResponseBody
    public Object checkName(String name, Long proId, String id) {
        Module module = null;
        module = moduleDao.findByNameAndProId(name, proId);

        Map<String, Object> map = new HashMap<String, Object>();
        if (!StringUtils.isEmpty(module)) {
            map.put("code", BaseConstant.STATE_CHONGFU);
            map.put("message", "已存在！");
        }
        return map;
    }

}
